/*! ******************************************************************************
 *
 * Created on 2022年6月11日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.spoon.navigation.design;

import org.eclipse.swt.layout.FormData;
import org.yzbdl.lanius.core.event.EventContext;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.spoon.navigation.design.classification.plugin.Plugin;
import org.pentaho.di.ui.spoon.navigation.design.listener.CategoryListener;
import org.pentaho.di.ui.spoon.navigation.design.listener.ClassificationListener;
import org.pentaho.di.ui.spoon.navigation.design.listener.InitedListener;
import org.pentaho.di.ui.spoon.navigation.design.listener.PluginListener;

/**
 * 核心对象树
 *
 * @author hs@yzbdl.ac.cn
 * @version 1.0
 * @date 2019-06-11 10:55
 */
public class DesignTree {

    private Spoon spoon;

    private TransTree transData;

    private JobTree jobData;

    public DesignTree(Spoon spoon, FormData layout) {
        super();
        this.spoon = spoon;
        this.jobData = new JobTree(spoon, layout);
        this.transData = new TransTree(spoon, layout);

        EventContext.registEventListener(new ClassificationListener(this));
        EventContext.registEventListener(new CategoryListener(this));
        EventContext.registEventListener(new PluginListener(this));
        EventContext.registEventListener(new InitedListener(this));
    }


    public void refreshSingleTrans(Plugin plugin){
        // 转换处于激活状态
        if(spoon.getActiveTransformation() != null){
            if (transData.isShow()) {
                transData.render(plugin);
            }
        }
    }
    
    public void refreshSingleJobs(Plugin plugin){
        // 转换处于激活状态
        if(spoon.getActiveJob() != null){
            if (jobData.isShow()) {
            	jobData.render(plugin);
            }
        }
    }

    /**
     * 刷新整个转换
     */
    public void refreshWholeTrans() {
        // 转换处于激活状态
        if(spoon.getActiveTransformation() != null){
            if (!transData.isShow()) {
                transData.show();
            }
            transData.render();
            transData.resetExpandByEntity();
        }
    }

    /**
     * 刷新整个作业
     */
    public void refreshWholeJob() {
        // 作业处于激活状态
        if(spoon.getActiveJob() != null){
            if (!jobData.isShow()) {
                jobData.show();
            }
            jobData.render();
            jobData.resetExpandByEntity();
        }
    }

    public void disposeTrans() {
        if(transData.isShow()){
            transData.hide();
            transData.dispose();
        }
    }

    public void disposeJob() {
        if(jobData.isShow()){
            jobData.hide();
            jobData.dispose();
        }
    }

    public void expandAll() {
        if (transData.isShow()) {
            transData.expandAll();
        }
        if (jobData.isShow()) {
            jobData.expandAll();
        }
    }

    public void collapseAll() {
        if (transData.isShow()) {
            transData.collapseAll();
        }
        if (jobData.isShow()) {
            jobData.collapseAll();
        }
    }

}
